<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Metrics
 *
 * @author QuangKhoi
 */
class Metrics {
    
    public static function getTrendlineData($_filter) {
        if ($_filter["streamId"] == '' || $_filter["dateFrom"] == '' || $_filter["dateTo"] == '') {
            return;
        }
        $language = 2;
        $stream = Stream::withId($_filter["streamId"]);
        $streamController = new StreamController($stream, new StreamView());
        $zTrendReader = new ZTrendReader($_filter["dateFrom"], $_filter["dateTo"], 7, $stream->query, $language);
        try {
            $zTrendCollection = $zTrendReader->read();
            if ($type == 'Day') {
                $zTrendCollection->dayTrend($infoArr, $trendArr);
            } else if ($type == 'Week') {
                $zTrendCollection->weekTrend($infoArr, $trendArr);
            } else {
                $zTrendCollection->monthTrend($infoArr, $trendArr);
            }
        } catch (Exception $e) {
            echo '<script>showError("#progress", ', json_encode($e->getMessage()), ');</script>';
            exit;
        }
        
        $result = array();
        $result["total"] = $streamController->count($_filter["dateFrom"], $_filter["dateTo"]);
        $date = "";
        $data = "";
        foreach($zTrendCollection->TrendItems as $zTrendItem) {
            $d = DateTime::createFromFormat("Ymd", $zTrendItem->getDate());
            $date .= $d->format('M d').',';
            $data .= $zTrendItem->getTrend().',';
        }
        $date = rtrim($date, ",");
        $data = rtrim($data, ",");
        $result["date"] = $date;
        $result["data"] = $data;
        return $result;
    }
    
    public static function getTopSocialPlatformsData($_filter) {
        //Do some stuff
        $language = 2;
        $stream = Stream::withId($_filter["streamId"]);
        $streamController = new StreamController($stream, new StreamView());
        $zSmtReader = new ZSmtReader($_filter["dateFrom"], $_filter["dateTo"], 7, $stream->query, $language);
        $data = $zSmtReader->read();
        $type = array();
        $posts = array();
        $authors = array();
        $threads = array();
        foreach($data->getItems() as $dt) {
            $type[] = $dt->getType();
            $posts[] = $dt->getNumberOfPosts();
            $authors[] = $dt->getNumberOfAuthors();
            $threads[] = $dt->getNumberOfThreads();
        }
        $sType = implode(",", $type);
        $sPost = implode(",", $posts);
        $sAuthor = implode(",", $authors);
        $sThread = implode(",", $threads);
        return array(
            'name' => 'Type;Posts;Authors;Threads',
            'data' => "$type[0];$posts[0];$authors[0];$threads[0]",
            'trend' => "$sType;$sPost;$sAuthor;$sThread"
        );
    }
        /*return array(
            'name' => 'Twitter;Blogs;Boards;News',
            'data' => '100071;5510;2548;1736',
            'trend' => '0,50000,80000,250000;590000,0,100000,250000;0,0,10000,250000;0,0,10000,250000'
        );*/
    
    public static function getSharingMetricsData($_filter) {
        $streamId = base64_decode($_filter["streamId"]);
        $db = Database::getInstance();
        $dateFrom = new DateTime($_filter["dateFrom"]);
        $dateTo = new DateTime($_filter["dateTo"]);
        $sDateFrom = $dateFrom->format("Y-m-d");
        $sDateTo = $dateTo->format("Y-m-d");
        $result = $db->query("SELECT sum(sharecount), sum(retweetcount) FROM signals WHERE streamid='$streamId' AND date >= '$sDateFrom' AND date <= '$sDateTo'");
        if ($result->returnCode == 200) {
            $row = $result->dataTable->rows[0];
            return array( 'name' => 'shares;retweets',
                'data' => $row[0].';'.$row[1]);
        }
        return array( 'name' => 'shares;retweets',
                'data' => '0;0');
    }
    
    public static function getSentimentData($_filter) {
        return array(
            'overal' => '21%;67%;12%',
            'date' => 'Sep 10;Sep 11;Sep 12;Sep 13;Sep 14;Sep 15;Sep 16',
            'data' => '50000,70000,80000;5000,140000,5000;200000,20000,30000;128000,40000,0;10000,140000,4000;140000,0,100000;250000,50000,50000'
        );
    }
    
    public static function getGenderDistributionData($_filter) {
        return array(
            'gender' => 'male;female;undefined',
            'data' => '50%;30%;20%'
        );
    }
    
    public static function getTopLanguagesData($_filter) {
        return array(
            'language' => 'English;Other;Japanese;Spanish;Thai;Vietnamese',
            'data' => '30%;12%;9%;5%;4%;1%'
        );
    }
    
    public static function getGeographicDistributionData($_filter) {
        return array(
            'location' => '1;2;3;4',
            'data' => '30%;20%;10%;4%'
        );
    }
    
    public static function getExposureData($_filter) {
        return array(
            'daily' => '63000000;174000000;52000',
            'date' => 'Sep 10;Sep 11;Sep 12;Sep 13;Sep 14;Sep 15;Sep 16',
            'data' => '100000;150000;250000;168000;154000;240000;350000'
        );
    }
    
    public static function getConversationMapData($_filter) {
        return array(
            'keyword' => 'iphone 5;iphone 5s;apple;iPhone 5C;gold;finerfrint;phone;video',
            'data' => '38%;36%;7%;168000;154000;240000;350000'
        );
    }
}

?>
